package com.bw.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.bw.dao.*;
import com.bw.pojo.system.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.bw.entity.PageResult;
import com.bw.service.system.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleResourceMapper roleResourceMapper;

    @Autowired
    private ResourceMapper resourceMapper;

    @Autowired
    private ResourceMenuMapper resourceMenuMapper;

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<Role> findAll() {
        return roleMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Role> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Role> roles = (Page<Role>) roleMapper.selectAll();
        return new PageResult<Role>(roles.getTotal(),roles.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<Role> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return roleMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Role> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<Role> roles = (Page<Role>) roleMapper.selectByExample(example);
        return new PageResult<Role>(roles.getTotal(),roles.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public Role findById(Integer id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param role
     */
    public void add(Role role) {
        roleMapper.insert(role);
    }

    /**
     * 修改
     * @param role
     */
    public void update(Role role) {
        roleMapper.updateByPrimaryKeySelective(role);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(Integer id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    public List<String> findAllPath(Integer id){
        Example example = new Example(RoleResource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleId",id);
        List<RoleResource> roleResources = roleResourceMapper.selectByExample(example);
        List<String> strings = new ArrayList<>();
        List<ResourceMenu> resourceMenus = resourceMenuMapper.selectAll();
        for (RoleResource roleResource : roleResources) {
            for (ResourceMenu resourceMenu : resourceMenus) {
                if(roleResource.getResourceId().equals(resourceMenu.getResourceId())){
                    strings.add(resourceMenu.getMenuId());
                }
            }

        }

        return strings;
    }

    @Override
    public List<Roles> findByIdList(List<Role> roleList) {
        List<Roles> rolesList = new ArrayList<Roles>();
        for (Role role : roleList) {
            Roles roles = new Roles();
            roles.setRole(role);
            //查询roleid,再根据resourceId去寻找resource列表
            Example example = new Example(RoleResource.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("roleId",role.getId());
            List<RoleResource> roleResources = roleResourceMapper.selectByExample(example);
            //获取resource列表
            List<Resource> resourceList = new ArrayList<Resource>();
            for (RoleResource roleResource : roleResources) {
                Resource resource = resourceMapper.selectByPrimaryKey(roleResource.getResourceId());
                resourceList.add(resource);
            }
            roles.setResourceList(resourceList);
            //添加到最后返回的roleList里
            rolesList.add(roles);
        }
        return rolesList;
    }

    public void updateRoleMenu(Integer id,List<String> strings){
        //先将roleResource删除
        Example example = new Example(RoleResource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleId",id);
        List<RoleResource> roleResources = roleResourceMapper.selectByExample(example);
        for (RoleResource roleResource : roleResources) {
            roleResourceMapper.delete(roleResource);
        }
        List<ResourceMenu> resourceMenus = resourceMenuMapper.selectAll();
        for (ResourceMenu resourceMenu : resourceMenus) {
            resourceMenuMapper.delete(resourceMenu);
        }

        //将roleResource封装好添加
        List<String> strings1 = new ArrayList<>();
        ResourceMenu resourceMenu = new ResourceMenu();
        for (String string : strings) {
            Menu menu = menuMapper.selectByPrimaryKey(string);
            Menu menu1 = menuMapper.selectByPrimaryKey(menu.getParentId());
            Integer integer = Integer.valueOf(menu1.getParentId());
            resourceMenu.setResourceId(integer);
            resourceMenu.setMenuId(string);
            resourceMenuMapper.insert(resourceMenu);
            if(!strings1.contains(menu1.getParentId())){
                strings1.add(menu1.getParentId());
            }
        }
        RoleResource roleResource = new RoleResource();
        for (String s : strings1) {
            Integer integer = Integer.valueOf(s);
            roleResource.setRoleId(id);
            roleResource.setResourceId(integer);
            roleResourceMapper.insert(roleResource);
        }
    }

    @Override
    public void updateRoles(Integer[][] list) {
        //先全部删除
        List<RoleResource> roleResources = roleResourceMapper.selectAll();
        for (RoleResource roleResource : roleResources) {
            roleResourceMapper.delete(roleResource);
        }
        ArrayList<Integer> integers = new ArrayList<Integer>();
        //roleList保存了role表的roleId
        List<Role> roleList = roleMapper.selectAll();
        for (Role role : roleList) {
            integers.add(role.getId());
        }


        for(int i=0;i<list.length;i++){
            for(int j=0;j<list[i].length;j++){
                RoleResource roleResource = new RoleResource();
                roleResource.setRoleId(integers.get(i));
                roleResource.setResourceId(list[i][j]);
                roleResourceMapper.insert(roleResource);
            }
        }
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Role.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 角色名称
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }

            // ID
            if(searchMap.get("id")!=null ){
                criteria.andEqualTo("id",searchMap.get("id"));
            }

        }
        return example;
    }

}
